package com.yld.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.yld.dto.DBDTO;

public class DBUtil {
	
	/**
	 * 获取数据库连接
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnection(){
		DBDTO dto = PropertiesUtil.parseJdbcFile();
		
		//2 连接数据库
		Connection connection=null;
		try {
			//1 加载驱动程序  连接数据库...
			Class.forName(dto.getDriver());
			connection = DriverManager.getConnection(dto.getUrl(), dto.getUser(), dto.getPassword());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return connection;
	}
	
	
	public static  void closePreparedStatement(PreparedStatement ps){
		if(null != ps){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static  void closeResultSet(ResultSet rs){
		if(null != rs){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static  void closeConnection(Connection connection){
		if(connection !=null){
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	

}
